package com.soft.testmybatis.mapper;

import com.soft.testmybatis.entity.Orders;
import com.soft.testmybatis.entity.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface OrdersMapper {

    // 通过订单编号查询订单信息
    Orders findOrderByNumber(String number);

    // 查询订单、订单明细的信息及下单的用户信息
    Orders findOrderWithDetailsAndUserById(int id);

    // 查询订单信息及其下单者信息
    @Select("select * from orders where id = #{id}")
    @Results({
            @Result(id = true,property = "id",column = "id"),
            @Result(property = "userId",column = "user_id"),
            @Result(property = "user",column = "user_id",javaType = User.class,
                    one = @One(select = "com.soft.testmybatis.mapper.UserMapper.findUserById"))
    })
    Orders findOrderAndUserById(int id);

    // 每个订单中包含的商品信息
    @Select("select * from orders where id = #{id}")
    @Results({
            @Result(id = true, property = "id", column = "id"),
            @Result(property = "userId", column = "user_id"),
            @Result(property = "orderDetails", column = "id",
                    javaType = List.class,
                    many = @Many(select = "com.soft.testmybatis.mapper.OrderDetailMapper.findOrderDetailsById"))
    })
    Orders findOrderWithDetailsById(int id);
}
